package com.ccl.iot.batch;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;

import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.dvr.utils.Consts;
import com.ccl.iot.privates.BitmapBuilder;
import com.ccl.iot.privates.SwitchIconBuilder;
import com.ccl.iot.IOTNetAPIEnum;
import com.ccl.iot.views.MarqueeLabel;
import com.ccl.iot.R;

public class MiniBatchView extends BatchView {
	private boolean iLabelVisible = true;
	private boolean iPressing = false;
	
	private Rect iRect = new Rect();
	private Rect iTempRect = new Rect();
	
	private Paint iImagePaint = new Paint();
	
	private Rect iLabelRect = new Rect();
	private MarqueeLabel iLabel = null;
	
	private Rect iItemRect = new Rect();
	
	public MiniBatchView(Context context, IOTBatch aBatch) {
		super(context, aBatch);
		
		iLabel = new MarqueeLabel(this.getContext());
		
		this.addView(iLabel);
		
		super.setOnTouchListener(iOnTouchListener);
		super.setOnClickListener(iOnClickListener);
	}
	
	public Rect getItemRect(){
		return iItemRect;
	}
	
	private void updateItemRect(){
		if (iRect != null && iRect.width() > 0 && iRect.height() > 0){
			int tWidthPadding, tHeightPadding;
			
			tWidthPadding = iRect.width() >> 4;
			tHeightPadding = iRect.height() >> 4;
			
			if (iLabelVisible){
				iLabelRect.set(tWidthPadding, iRect.height() - iLabel.getFontHeight(), iRect.width() - tWidthPadding, iRect.height());
				
				iItemRect.set(tWidthPadding,  tHeightPadding,  iRect.width() - tWidthPadding, iRect.height() - iLabelRect.height() - tHeightPadding);
			}else{
				iItemRect.set(tWidthPadding,  tHeightPadding,  iRect.width() - tWidthPadding, iRect.height() - tHeightPadding);
			}
		}else{
			iItemRect.set(iRect);
		}
		
		this.postInvalidate();
	}
	
	public void setLabelVisibility(boolean aVisible){
		if (iLabelVisible != aVisible){
			iLabelVisible = aVisible;
			
			if (iLabel != null){
				iLabel.setVisibility(aVisible ? VISIBLE : GONE);
			}
			
			updateItemRect();
		}
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		
		iTempRect.set(l, t, r, b);
		
		if (! iTempRect.equals(iRect)){
			iRect.set(iTempRect);
			
			updateItemRect();
			
			if (iLabelVisible){
				iLabel.layout(iLabelRect.left, iLabelRect.top, iLabelRect.right, iLabelRect.bottom);
			}
		}		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		if (iRect.width() > 0 && iRect.height() > 0){
			if (iItemRect != null){
				Bitmap tSwitch = null;
				
				tSwitch = SwitchIconBuilder.Build(iItemRect.width(), iItemRect.height(), true, false, iPressing, IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_OWNER, Consts.BATCH_IMG_IDS.get(getItem().getImgIndex()));
				
				if (tSwitch != null){
					canvas.drawBitmap(tSwitch, iItemRect.left, iItemRect.top, iImagePaint);
				}else{
					iImagePaint.setColor(Color.DKGRAY);
					
					canvas.drawRect(iItemRect, iImagePaint);
				}
			}
			
			if (iLabelVisible && getItem() != null){
				String tLabel = getItem().getName();
				
				iLabel.setText(tLabel);
			}
		}
	}
	
	@Override
	protected Rect calcRect() {
		Rect tRect = BitmapBuilder.GetBitmapRect(this.getContext(), R.drawable.icon_unknown);
		
		if (iLabelVisible){
			tRect.bottom += iLabelRect.height();
		}
		
		return tRect;
	}
	
	private View.OnClickListener iExOnClickListener = null;
	@Override
	public void setOnClickListener(OnClickListener l) {
		iExOnClickListener = l;
	}

	private View.OnClickListener iOnClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if (getVisibility() == VISIBLE){
				getItem().execute();
			}
			
			if (iExOnClickListener != null){
				iExOnClickListener.onClick(v);
			}
		}
	};

	private View.OnTouchListener iOnTouchListener = new View.OnTouchListener() {
		float moveY = -1;
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (getItem() != null){
				if (event != null){
					int tAction = event.getAction();
					if (tAction == MotionEvent.ACTION_DOWN){
						moveY = -1;
						Rect tRect = getItemRect();

						if (tRect != null){
							if (event.getX() >= tRect.left && event.getY() >= tRect.top && event.getX() < tRect.right && event.getY() < tRect.bottom){
								iPressing = true;
							}
						}
						requestDisallowInterceptTouchEvent(true);
					}else if (tAction == MotionEvent.ACTION_UP){
						moveY = -1;
						if (iPressing){
							iPressing = false;
						}
					}else if (tAction == MotionEvent.ACTION_CANCEL){
						moveY = -1;
						iPressing = false;
					}else if (tAction == MotionEvent.ACTION_MOVE){
						
						requestDisallowInterceptTouchEvent(true);
						if(moveY == -1){
							moveY = event.getY();
						}
						if(moveY == event.getY()){
							requestDisallowInterceptTouchEvent(true);
						}else{
							if(Math.abs(moveY - event.getY()) <= 10){
								requestDisallowInterceptTouchEvent(true);
							}else{
								requestDisallowInterceptTouchEvent(false);
							}
							
						}
						
					}else{
						return false;
					}
					
					postInvalidate();
										
					return true;
				}
			}
			
			return false;
		}
	};

	@Override
	public void onNameChanged(IOTDevice aSource, String aNewName) {

	}
}
